package servlet;

import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.alipay.api.AlipayApiException;
import com.alipay.api.internal.util.AlipaySignature;
import com.alipay.demo.trade.config.Configs;

import common.ServerResponse;
import service.IOrderService;
import service.IPayService;
import service.IShoppingcartService;
import service.impl.OrderServiceImpl;
import service.impl.PayServiceImpl;
import service.impl.ShoppingcartServiceImpl;
import util.myUtil;

@WebServlet("/pay")
public class PayServlet extends HttpServlet{
	private static final long serialVersionUID = 1L;
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		System.out.println("进入dopost");
		doGet(request, response);
	}
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		myUtil.setHead(request, response);
		System.out.println("进入servlet");
		String type=request.getParameter("type");
		System.out.println("type:"+type);
		if("pay".equals(type)) {
			pay(request,response);
		}
		else if("alipay_callback".equals(type)) {
			callback(request,response);
		}
		else if("query_order_pay_status".equals(type)) {
			query_order_pay_status(request,response);
		}
		
	}
	
	
	/***1.1支付***/
	public void pay(HttpServletRequest request, HttpServletResponse response) {
		String order_no=request.getParameter("order_no");
		//从session中获取user_id
		HttpSession session = request.getSession();
		Object obj_session_user_id = session.getAttribute("id");
		//执行支付逻辑
		IPayService p=new PayServiceImpl();
		ServerResponse se = p.pay(obj_session_user_id,order_no);
		//将ServerResponse转为json并通过流写出
		myUtil.getjson(se,response);
	}
	/***1.3支付宝服务器回调应用服务器***/
	public ServerResponse callback(HttpServletRequest request, HttpServletResponse response) {
		
		System.out.println("=====支付宝服务器回调应用服务器=====");
		
		Map<String,String[]> params=request.getParameterMap();
		Map<String,String> requestparams=new HashMap<String, String>();
		Iterator<String> it=params.keySet().iterator();
		while(it.hasNext()) {
			String key=it.next();
			String[] strArr=params.get(key);
			String value="";
			for(int i=0;i<strArr.length;i++) {
				value=(i==strArr.length-1)? value+strArr[i]:value+strArr[i]+",";
			}
			requestparams.put(key, value);
		}
		
//		//1.支付宝验证签名
//		try {
//			requestparams.remove("sign_type");
//			boolean result=AlipaySignature.rsaCheckV2(requestparams, 
//									   Configs.getAlipayPublicKey(), 
//									   "utf-8", 
//									   Configs.getSignType());
//			if(!result) {
//				return ServerResponse.serverResponseByError("非法请求，验证不通过");
//			}
//			//处理业务逻辑
//			
//		} catch (AlipayApiException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}

		IPayService p=new PayServiceImpl();
		ServerResponse se = p.callback(requestparams);
		return se;
	}
	/***1.2查询订单支付状态***/
	public void query_order_pay_status(HttpServletRequest request, HttpServletResponse response) {
		String order_no = request.getParameter("order_no");
		//从session中获取user_id
		HttpSession session = request.getSession();
		Object obj_session_user_id = session.getAttribute("id");
		//执行创建订单逻辑
		IPayService p=new PayServiceImpl();
		ServerResponse se = p.query_order_pay_status(obj_session_user_id,order_no);
		//将ServerResponse转为json并通过流写出
		myUtil.getjson(se,response);
	}
}
